<?php
namespace app\common\model;

use app\common\Model;
use App\common\utils\builder\FormBuilder;
use App\common\utils\builder\ListBuilder;

class Admin extends Model
{
    // 自动时间戳
    protected $autoWriteTimestamp = 'datetime';
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    
    // 一对一（查询对应角色）
    public function role()
    {
        return $this->hasOne(AdminRole::class, 'id', 'role_id');
    }

    /**
     * 获取列表视图
     *
     * @return ListBuilder
     */
    public function showListView(): ListBuilder
    {
        $builder = new ListBuilder;
        $builder = $builder
            ->addColumn('id', '序号', [
                'width'             => '80',
            ])
            ->addColumn('ctime', '添加时间', [
                'width'             => '180',
            ])
            ->addColumn('admin_role', '所属角色')
            ->addColumn('headimg', '头 像', [
                'type'              => 'image',
            ])
            ->addColumn('username', '账 号')
            ->addColumn('nickname', '昵 称')
            ->addColumn('status_text', '状 态', [
                'width'             => '100'
            ])
            ->addColumn('last_login_ip', '最近登录IP')
            ->addColumn('last_login_time', '最近登录时间');
        return $builder;
    }

    /**
     * 显示表单数据
     *
     * @return FormBuilder
     */
    public function showFormView() : FormBuilder
    {
        $roleModel = new AdminRole;
        $roleList = $roleModel->getRoleOptions();
        $builder = new FormBuilder;
        $builder = $builder
            ->addRow('role_id', 'select', '所属角色', '', [
                'options'       => $roleList,
                'col'               => [
                    'span'          => 12
                ],
            ])
            ->addRow('status', 'radio', '当前状态', 1, [
                'options'       => [
                    [
                        'label' => '锁定',
                        'value' => 0,
                    ],
                    [
                        'label' => '正常',
                        'value' => 1,
                    ],
                ],
                'col'               => [
                    'span'          => 12
                ],
            ])
            ->addRow('nickname', 'input', '用户昵称', '', [
                'col'               => [
                    'span'          => 12
                ],
            ])
            ->addRow('headimg', 'custom', '账号头像', '', [
                'type'              => 'uploadify',
                'extra'             => [
                    'props'         => [
                        'format'    => 'image',
                        'btn_text'  => '选择头像',
                        'limit'     => 1,
                        'ext'       => 'jpg,png,gif',
                    ],
                    'col'               => [
                        'span'          => 12
                    ],
                ],
            ])
            ->addRow('username', 'input', '登录账号', '', [
                'col'               => [
                    'span'          => 12
                ],
            ])
            ->addRow('password', 'input', '登录密码', '', [
                'col'               => [
                    'span'          => 12
                ],
            ]);
        return $builder;
    }

    /**
     * 查询单条数据
     *
     * @param array $map
     * @return void
     */
    public function findFirst(array $map)
    {
        $data = $this->where($map)->first();
        if (!$data) {
            return false;
        }
        $data = $data->toArray();
        $web_url = getHpConfig('web_url');
        if ($data['headimg']) {
            $data['headimg'] = "{$web_url}/{$data['headimg']}";
        }
        unset($data['password']);
        return $data;
    }
}
